package com.apollow.oschina.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apollow.oschina.R;
import com.apollow.oschina.adapter.RecommedAdapter;
import com.apollow.oschina.base.BaseFragment;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.BlogBean;
import com.apollow.oschina.protocol.RecommendProtocol;
import com.apollow.oschina.view.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by bobo on 2017/1/12.
 */
public class RecommendFragment extends BaseFragment {
    public RecommedAdapter mBlogtAdapter;
    public RecommendProtocol mProtocol;
    public List<BlogBean> blogBeen;
    @BindView(R.id.recycleView)
    RecyclerView mRecycleView;
    @BindView(R.id.swipe_reflash)
    SwipeRefreshLayout mSwipeReflash;
    public int pageIndex;
    private LinearLayoutManager mLayoutManager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = Global.layoutInflate(R.layout.fragment_base);
        ButterKnife.bind(this, view);
        mProtocol = new RecommendProtocol();
        mBlogtAdapter = new RecommedAdapter(getActivity(),mProtocol);
        mRecycleView.setAdapter(mBlogtAdapter);
        mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRecycleView.setLayoutManager(mLayoutManager);
        mRecycleView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL));
        ButterKnife.bind(this, view);
        return view;
    }
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initEvent();
    }
    private void initEvent() {
        mSwipeReflash.setColorSchemeColors(Color.GREEN, Color.BLACK, Color.CYAN, Color.BLUE, Color.YELLOW);
        mSwipeReflash.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Global.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBlogtAdapter.blogBeen.clear();
                        mProtocol.setPageIndex(String.valueOf(++pageIndex));
                        System.out.println(pageIndex);
                        initData();
                        mSwipeReflash.setRefreshing(false);
                        Global.showToast("刷新完成");
                    }
                }, 1500);
            }
        });
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int page = 0;
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
                //加载更多
                if ((blogBeen.size()-1) == lastVisibleItem){
                    /*if (page < 4){
                        page++;
                    }*/
//                    initData(page+"");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            blogBeen.addAll(mProtocol.getData().getBlogs());
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBlogtAdapter.setBlogBeen(blogBeen);
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                blogBeen = mProtocol.getData().getBlogs();
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mBlogtAdapter.setBlogBeen(blogBeen);
                    }
                });
            }
        }).start();
    }

}
